const Error = require('./error')
const storage = require('./storage.js')
const request = require('./request')

const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isArray
} = require('./dataType.js')

const maintenanceOrganization = class maintenanceOrganization {

    constructor() {}

    save(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.id) && isString(options.id)) {
                params.id = options.id;
            }
            if(!isUndefined(options.name) && isString(options.name)) {
                params.name = options.name;
            }
            if(!isUndefined(options.synopsis) && isString(options.synopsis)) {
                params.synopsis = options.synopsis;
            }
            if(!isUndefined(options.telephone) && isString(options.telephone)) {
                params.telephone = options.telephone;
            }
            if(!isUndefined(options.headPhone) && isString(options.headPhone)) {
                params.headPhone = options.headPhone;
            }
            if(!isUndefined(options.headName) && isString(options.headName)) {
                params.headName = options.headName;
            }
            if(!isUndefined(options.address) && isString(options.address)) {
                params.address = options.address;
            }
            if(!isUndefined(options.areaCode) && isString(options.areaCode)) {
                params.areaCode = options.areaCode;
            }
            if(!isUndefined(options.longitude) && isNumber(options.longitude)) {
                params.longitude = options.longitude;
            }
            if(!isUndefined(options.latitude) && isNumber(options.latitude)) {
                params.latitude = options.latitude;
            }
            if(!isUndefined(options.status) && isBoolean(options.status)) {
                params.status = options.status;
            }
            return new Promise((resolve, reject) => {
                request(MS._config.parameters.MRO.SAVE_MRO.url,
                    MS._config.parameters.MRO.SAVE_MRO.method, params).then(res => {
                    resolve(res.data)
                }).catch(err => {
                    reject(err)
                })
            })
        }
    }

    remove(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MRO.REMOVE_MRO.url,
                MS._config.parameters.MRO.REMOVE_MRO.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    get(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MRO.GET_MRO.url,
                MS._config.parameters.MRO.GET_MRO.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if(!isUndefined(options)) {
            if(!isUndefined(options.name) && isString(options.name)) {
                params.name = options.name;
            }
            if(!isUndefined(options.areaCode) && isString(options.areaCode)) {
                params.areaCode = options.areaCode;
            }
            if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum;
            }
            if(!isUndefined(option.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MRO.QUERY_MRO.url,
                MS._config.parameters.MRO.QUERY_MRO.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

}

module.exports = maintenanceOrganization